﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using MySQL.Db;

namespace Gollie.DataTables
{
	public class ReviewsItem
	{
		public DateTime ReviewDate { get; set; }
		public uint Rating { get; set; }
		public string Comment { get; set; }
	}

	public class Reviews : BaseBindable<ReviewsItem>
	{
		internal double AverageRating = 0;

		internal uint[] Ratings = new uint[5] {0,0,0,0,0};
		internal uint SumRating = 0;

		public Reviews(uint vendorId, DateTime from, DateTime to)
		{
			ResultSet resultSet = Database.Queries.GetReviews(vendorId, from, to); //there's a limit in the sp

			this.AddDetails(resultSet);
		}

		private void AddDetails(ResultSet resultSet)
		{
			int numRows = resultSet.Count;

			if (numRows == 0)
			{
				AverageRating = 0;

				return;
			}

			for (int i = 0; i < numRows; i++)
			{
				uint rating = resultSet.GetUInt32(i, "rating");
				string comment = resultSet.GetString(i, "comment");

				this.Add(new ReviewsItem()
				{
					ReviewDate = resultSet.GetDateTime(i, "reviewdate"),
					Comment = comment ?? "",
					Rating = rating
				});

				if (rating <= 5 && rating > 0)
					Ratings[rating - 1]++;

				SumRating += rating;
			}

			AverageRating = ((double)SumRating) / numRows;
		}
	}
}